package it.asvp.asvp_portal.service.services.impl;

import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.dao.MemberDao;
import it.asvp.asvp_portal.data.dao.PartnerDao;
import it.asvp.asvp_portal.data.entity.domain.Address;
import it.asvp.asvp_portal.data.entity.domain.Partner;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.service.services.MemberAccountService;
import it.asvp.asvp_portal.service.services.PartnerService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PartnerServiceImpl implements PartnerService{

	@Autowired
	PartnerDao partnerDao;
	@Autowired
	MemberDao memberDao;
	@Autowired
	MemberAccountService memberAccountService;
	
	@Transactional
	@Rollback
	public Long createPartner(Long idCurrentAccount,Partner transientPartner) throws Exception{
		
		transientPartner.setCreatedOn(DateUtils.getCurrentDate());
		transientPartner.setLastUpdate(DateUtils.getCurrentDate());
		
		Member persistentMember=memberDao.findById(idCurrentAccount);
		transientPartner.setMember(persistentMember);
		return partnerDao.persist(transientPartner);
	}
	
	@Transactional
	@Rollback
	public void updatePartner(Long idCurrentAccount, Long idPartnerToUpdate, Partner transientPartner) throws Exception{
		
		Partner persistentPartner=partnerDao.findById(idPartnerToUpdate);
		
		if(persistentPartner==null){
			throw new Exception("Partner non esistente");
		}
		Address persistentAddress=persistentPartner.getAddress();
        persistentAddress.setCity(transientPartner.getAddress().getCity());
        persistentAddress.setCivicNumber(transientPartner.getAddress().getCivicNumber());
        persistentAddress.setLastUpdate(DateUtils.getCurrentDate());
        persistentAddress.setPostalCode(transientPartner.getAddress().getPostalCode());
        persistentAddress.setProvince(transientPartner.getAddress().getProvince());
        persistentAddress.setStreet(transientPartner.getAddress().getStreet());
        
        persistentPartner.setAddress(persistentAddress);
        persistentPartner.setDescription(transientPartner.getDescription());
        persistentPartner.setEmail(transientPartner.getEmail());
        persistentPartner.setLastUpdate(DateUtils.getCurrentDate());
        persistentPartner.setName(transientPartner.getName());
        persistentPartner.setWebsiteLink(transientPartner.getWebsiteLink());
        
        partnerDao.merge(persistentPartner);
	}
	
	@Transactional
	@Rollback
	public void deletePartner(Long idCurrentAccount, Long idPartnerToDelete) throws Exception{
		
			Partner persistentPartner=partnerDao.findById(idPartnerToDelete);
			
			if(persistentPartner==null){
				throw new Exception("Partner non esistente");
			}
			
			partnerDao.delete(persistentPartner);	
	}
	
	@Transactional
	@Rollback
	public Partner getById(Long idPartner) throws Exception{
		
		Partner persistentPartner=partnerDao.findById(idPartner);
		  if(persistentPartner==null){
			  throw new Exception("Partner non presente");
		  }
		  return persistentPartner;
	}
	
	@Transactional
	@Rollback
	public List<Partner> getAllPartners(){
		
		List<Partner> allPartners=partnerDao.findAll();
		return allPartners;
	}
}
